const util = require('../../utils/util1');
const app = getApp();

let o;

Page({
  data: {
   /* action: {
      method: 'play'
    },*/
    bgImages:'/img/imgs/wishes-panel.png',
    toname: '',
    sex: 1,
    today: '',
    sentday: '',
    sentence: '',
    wishesId: '',
    state: 0, //0 换一个, 1 制作我的祝福话
    showOverlay: false,
    imgid:'',
    imgurl:'',
    imgadio:'',
    isimg: true,
    tempFilePaths:'',
    tempFilePathsID: '',
   // wishName:''
  },
  finishCard () {
    this.setData({
      state: '1'
    })
  },
  changeOne (e) {
    //console.log(e)
    // 点击事件，强制拉去数据
    let that = this;
    let _wishes = app.getWishes();
    if (_wishes){
      this.setData({
        sentence: _wishes
      })
    }
  },
  /**监控返回 */
  onUnload:function () {
    var that=this
    app.setimgid('', true)
    app.setimgurl('', true)
    app.setimgadio('', true)
    console.log(that.data.userInfo)
    console.log(app.globalData.userInfo)
    var userInfo2 = that.data.userInfo
    var userInfo3 = app.globalData.userInfo
    userInfo2.nickName = userInfo3.nickName;
    app.setUserInfo(userInfo2);
    app.setWishes('');
    app.setToName('')
    app.settempFilePathsID('')
    app.settempFilePaths('')
    app.globalData.bgImages=''
    //app.globalData.wishName=''
  },
  shareTips () {
    let that = this;
    this.setData({
      showOverlay: true
    })
    setTimeout(function() {
      that.hideOverlay()
    }, 1500);
  },
  hideOverlay () {
    this.setData({
      showOverlay: false
    })
  },
  // 调整到制作页面
  customCard (e) {
    app.globalData.toIndex = 2
    wx.switchTab({
      url: '/pages/toptaber/toptaber',
      success:function(e){
        console.log(e)
      }
    })
  },
  //跳转到自定义页面
  bindViewTap(){
    if (this.data.state === '0') {
      app.setWishes(this.data.sentence);
      //var audioCtx = wx.createAudioContext('myAudio')
      //audioCtx.pause()
      wx.navigateTo({
        url: `/pages/custom/custom`
      })
      //audioCtx.setSrc(app.globalData.imgadio)
      //audioCtx.play()
      //app.setAudioPlay(audioCtx)
      //url: `/pages/custom/custom?relation=${this.data.relation}&sex=${this.data.sex}`
    }
  },
  onLoad (options) {
    var that=this
   console.log('options', options)
    o = options;
    var istrue = 0;
   // console.log("istrue",istrue)
    var Interval= setInterval(function () {
      console.log("istrue", istrue)
      //循环执行代码  
      istrue = istrue + 1;
      if (istrue > 10) {
        console.log("isimg", that.data.isimg)
        that.setData({
          isimg: false
        })
        clearInterval(Interval)
      }
    }, 1000) //循环时间 这里是1秒   
    
  },
  onShow(){
    let that = this;
    console.log('sssssss')

   /*if (app.globalData.audioPlay){
      var audioCtx = wx.createAudioContext('myAudio')
      audioCtx.setSrc(app.globalData.imgadio)
      //audioCtx.pause()
      //audioCtx.play()
      //app.setAudioPlay(audioCtx)
    }*/

    if (o.state === '0') {
      console.log('进入制作页面')
      app.clearWishes(true);
      app.clearTempNickName();
      app.clearTempToName();
      // 初始化数据
      this.setData({
        toname: app.getToName(),
        state: o.state,
        today : util.today(),
        imgid: app.globalData.imgid,
        imgurl: app.globalData.imgurl,
        imgadio: app.globalData.imgadio,
        tempFilePaths: app.globalData.tempFilePaths,
        tempFilePathsID: app.globalData.tempFilePathsID,
        //wishName: app.globalData.wishName
      })
      this.setData({
        'action.method': 'play'//data没有定义action，使用这种赋值方式  
      })
      console.log("this tempFilePathsID:" + that.data.tempFilePathsID + " app tempFilePathsID:" + app.globalData.tempFilePathsID)
      // 获取祝福话
      this.changeOne();

      // 获取用户信息
      app.getUserInfos(function(userInfo){
        that.setData({
          userInfo:userInfo
        })
      })

        console.log("bgImages:"+app.globalData.bgImages)
        console.log("imgadio" + app.globalData.imgadio)
      if (app.globalData.bgImages){
        that.setData({
          bgImages: app.globalData.bgImages
        })
      }
      /*if (app.globalData.istwoOnload === "1"){
        app.globalData.istwoOnload = "0"
        var ct={state: "0" }
        that.onLoad(ct)
      }*/
    }

    if (o.state === '1') {
      console.log('接收贺卡')
      console.log(o)
      this.setData({
          state: o.state,
          toname: o.toname,
          fromname: o.fromname,
          fromavatar: o.fromavatar,
          sentday: o.sentday,
          sentence: o.sentence,
          imgid: o.imgid,
          imgurl: o.imgurl,
          imgadio: o.imgadio,
          tempFilePathsID: o.tempFilePathsID,
          bgImages: o.bgImages,
          //wishName: o.wishName
      })
      this.setData({
        'action.method': 'play'//data没有定义action，使用这种赋值方式  
      })
      if (that.data.tempFilePathsID !=''){
        wx.request({
          url: 'https://wsh.weyifeng.com/getOneimg',
          data: { tempFilePathsID: that.data.tempFilePathsID },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            console.log(res)
            that.setData({
              tempFilePaths: res.data.advertImg.imgUrl
            })
          }
        }) 
      }
    }

    //判断是否需要展示 点击跳转至自定义页面的提示
    let preview_custom_hint = wx.getStorageSync('preview-custom-hint') || false;
    this.setData({
      showCustomHint : preview_custom_hint
    })
  },
  onShareAppMessage () {
    var sentences=this.data.sentence
    if (!sentences){
      sentences='身体健康!'
    }
    this.hideOverlay()
    return {
      title: `${this.data.userInfo.nickName}给您发来祝福`,
      desc: "你也可以制作祝福话送给TA哟！",
      path: '/pages/preview/preview?&state=1&fromavatar=' + this.data.userInfo.avatarUrl + '&toname=' + this.data.toname + '&fromname=' + this.data.userInfo.nickName + '&sentday=' + this.data.today + '&sentence=' + sentences + '&imgid=' + this.data.imgid + '&imgurl=' + this.data.imgurl + '&imgadio=' + this.data.imgadio + '&tempFilePathsID=' + this.data.tempFilePathsID + '&bgImages=' + this.data.bgImages
    }
    if (options.from === 'button') {
      return {
        title: `${this.data.userInfo.nickName}给您发来祝福`,
        desc: "你也可以制作祝福话送给TA哟！",
        path: '/pages/preview/preview?&state=1&fromavatar=' + this.data.userInfo.avatarUrl + '&toname=' + this.data.toname + '&fromname=' + this.data.userInfo.nickName + '&sentday=' + this.data.today + '&sentence=' + sentences + '&imgid=' + this.data.imgid + '&imgurl=' + this.data.imgurl + '&imgadio=' + this.data.imgadio + '&tempFilePathsID=' + this.data.tempFilePathsID + '&bgImages=' + this.data.bgImages
      }
    }
  },
  confirmCustomHint : function(){
    wx.setStorageSync('preview-custom-hint',true);
    this.setData({
      showCustomHint:true
    }) 
  }
})
